package com.ibeetl.admin.console.web;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.*;
import com.ibeetl.admin.core.service.*;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
import com.sun.javafx.collections.MappingChange;
import javafx.scene.input.DataFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.assertj.core.util.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 用户管理接口
 *
 * @author luhao
 */
@Controller
public class CheckController {
	private final Log log = LogFactory.getLog(this.getClass());
	private static final String MODEL = "/admin/check";

	@Autowired
	FiDataTransDetailService fiDataTransDetailService;

	@Autowired
	CoreGlInterfaceService coreGlInterfaceService;

	@Autowired
	FiFinItemCheckDetailService fiFinItemCheckDetailService;

	@Autowired
	FiDataCheckDetailService fiDataCheckDetailService;

	@Autowired
	FiDataCheckDetailBakService fiDataCheckDetailBakService;

	@Autowired
	FiItemTransService fiItemTransService;



	@Autowired
	FiCodeTransService fiCodeTransService;
	private List<String> jyList = new ArrayList<>();
	/* 页面 */

	@GetMapping(MODEL + "/index.do")
	@Function("check")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("/admin/check/index.html");
		return view;
	}


	/* 开始数据抽取 */
	@PostMapping(MODEL + "/check.json")
	@ResponseBody
	public JsonResult<String> check(@Validated(ValidateConfig.ADD.class) CoreSearch search) {
		if (search.getBusType() == "" || search.getManageCom() == "" || search.getVersionNo() == "" || search.getStartDate() == "") {
			return JsonResult.failMessage("请输入完整查询条件");
		}
		String startDate = fiDataTransDetailService.getDate(search.getDateRange(), "0").toString();
		String endDate = fiDataTransDetailService.getDate(search.getDateRange(), "1").toString();
		search.setStartDate(startDate);
		search.setEndDate(endDate);
		List<DataCheckRules> rules = fiDataTransDetailService.queryVersionRules(search);
		DataCheckRules rule = null;
		List<FiItemTrans> fiItemTranss = fiItemTransService.getFiItemTransDatas();
		List<FiCodeTrans> fiCodeTranss = fiCodeTransService.getFiCodeTransDatas();
		Map<String, String> fiManagecomMap = new HashMap<>();
		Map<String, String> bankAccMap = new HashMap<>();
		Map<String, String> proYearMap = new HashMap<>();
		Map<String, String> rateFlagMap = new HashMap<>();
		Map<String, String> capaccMap = new HashMap<>();
		Map<String, String> saleChnlMap = new HashMap<>();
		Map<String, String> feeTypeMap = new HashMap<>();
		Map<String, String> manegecomMap = new HashMap<>();

		// 将专项转换值根据codetype进行分类
		for (int n = 0; n < fiCodeTranss.size(); n++) {
			FiCodeTrans fiCodeTrans = fiCodeTranss.get(n);
			if ("managecom".equals(fiCodeTrans.getCodeType())) {
				fiManagecomMap.put(fiCodeTrans.getCode(), fiCodeTrans.getCodeAlias());
			} else if ("bankaccno".equals(fiCodeTrans.getCodeType())) {
				bankAccMap.put(fiCodeTrans.getCode(), fiCodeTrans.getCodeAlias());
			} else if ("proyear".equals(fiCodeTrans.getCodeType())) {
				proYearMap.put(fiCodeTrans.getCode(), fiCodeTrans.getCodeAlias());
			} else if ("rateflag".equals(fiCodeTrans.getCodeType())) {
				rateFlagMap.put(fiCodeTrans.getCode(), fiCodeTrans.getCodeAlias());
			} else if ("capacc".equals(fiCodeTrans.getCodeType())) {
				capaccMap.put(fiCodeTrans.getCode(), fiCodeTrans.getCodeAlias());
			} else if ("salechnl".equals(fiCodeTrans.getCodeType())) {
				saleChnlMap.put(fiCodeTrans.getCode(), fiCodeTrans.getCodeAlias());
			} else if ("feetype".equals(fiCodeTrans.getCodeType())) {
				feeTypeMap.put(fiCodeTrans.getCode(), fiCodeTrans.getCodeAlias());
			}
		}
		try {
			//参数处理
			String manageCom = search.getManageCom();
			String busType = search.getBusType();
			String versionNo = search.getVersionNo();
			String segment1 = fiManagecomMap.get(manageCom);
			CoreSearch coreSearch = new CoreSearch();
			coreSearch.setManageCom(segment1);
			coreSearch.setStartDate(startDate);
			coreSearch.setEndDate(endDate);
			coreSearch.setBusType(search.getBusType());
			coreSearch.setVersionNo(search.getVersionNo());

			//业务凭证表/财务凭证表历史提取校验结果删除
			fiDataTransDetailService.deleteFromFiDataTransDetail(coreSearch);
			coreGlInterfaceService.deleteFromCoreGlInterface(coreSearch);

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm:ss");

			for (int i = 0; i < rules.size(); i++) {

				rule = rules.get(i);
				String mainSQL = rule.getMainSql();
				String assistSQL = rule.getAssistSql();
				//在这里替换参数
				//替换业务提数逻辑参数
				mainSQL = mainSQL.replaceAll("#manageCom#", manageCom);
				mainSQL = mainSQL.replaceAll("#startDate#", startDate);
				mainSQL = mainSQL.replaceAll("#endDate#", endDate);


				//替换财务提取逻辑参数
				assistSQL = assistSQL.replaceAll("#manageCom#", segment1);
				assistSQL = assistSQL.replaceAll("#startDate#", startDate);
				assistSQL = assistSQL.replaceAll("#endDate#", endDate);


				//校验类型checktype为01则科目校验规则,02则为其他
				if ("01".equals(rule.getCheckType())) {
					// 提取核心记账结果
					List<CoreGlInterface> coreGlInterfaceDatas = coreGlInterfaceService.queryData(assistSQL);
					for (int j = 0; j < coreGlInterfaceDatas.size(); j++) {
						CoreGlInterface coreGlInterfaceData = coreGlInterfaceDatas.get(j);
						coreGlInterfaceData.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
						coreGlInterfaceData.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
						coreGlInterfaceService.saveCoreGlInterFace(coreGlInterfaceData);
					}

					//核心业务数据提取
					List<FiDataTransDetail> fiDataTransDetailDatas = fiDataTransDetailService.queryData(mainSQL);
					System.out.println(mainSQL);
					System.out.println(fiDataTransDetailDatas.size());
					for (int j = 0; j < fiDataTransDetailDatas.size(); j++) {
						//根据记账科目进行专项转换
						FiDataTransDetail fiDataTransDetail = fiDataTransDetailDatas.get(j);
						String accountCode = fiDataTransDetail.getAccountCode();
						FiItemTrans fiItemTrans = new FiItemTrans();

						for (int n = 0; n < fiItemTranss.size(); n++) {
							fiItemTrans = fiItemTranss.get(n);
							if (accountCode .equals( fiItemTrans.getAccountCode())) {
								// 机构段转换
								if ("0".equals(fiItemTrans.getFiManageCom())) {
									fiDataTransDetail.setFiManageCom("0");
								} else {
									fiDataTransDetail.setFiManageCom(fiManagecomMap.get(fiDataTransDetail.getManageCom()));
								}
								// 异地机构段转换
								if ("0".equals(fiItemTrans.getFiExecuteCom())) {
									fiDataTransDetail.setFiExecuteCom("0");
								} else {
									fiDataTransDetail.setFiExecuteCom(fiManagecomMap.get(fiDataTransDetail.getFiExecuteCom()));
								}
								//银行段转换
								if ("0".equals(fiItemTrans.getFiBankCode())) {
									fiDataTransDetail.setFiBankCode("0");
								} else {
									fiDataTransDetail.setFiBankCode(bankAccMap.get(fiDataTransDetail.getFiBankCode()));
								}
								//产品期限
								if ("0".equals(fiItemTrans.getFiProyear())) {
									fiDataTransDetail.setFiProyear("0");
								} else {
									fiDataTransDetail.setFiProyear(proYearMap.get(fiDataTransDetail.getFiRiskCode()));
								}
								//应免税标识
								if ("0".equals(fiItemTrans.getFiRateFlag())) {
									fiDataTransDetail.setFiRateFlag("0");
								} else {
									fiDataTransDetail.setFiRateFlag(rateFlagMap.get(fiDataTransDetail.getFiRiskCode()));
								}
								//资金账户
								if ("0".equals(fiItemTrans.getFiCapacc())) {
									fiDataTransDetail.setFiCapacc("0");
								} else {
									fiDataTransDetail.setFiCapacc(capaccMap.get(fiDataTransDetail.getFiRiskCode()));
								}

								//缴费期限
								if ("0".equals(fiItemTrans.getFiPayYears())) {
									fiDataTransDetail.setFiPayYears("0");
								} else {  //添加pyintv字段
									if ("0".equals(fiDataTransDetail.getPayIntv())) {
										fiDataTransDetail.setFiPayYears("01");
									}
									if ("-1".equals(fiDataTransDetail.getPayIntv())) {
										fiDataTransDetail.setFiPayYears("99");
									}
								}

								// 保单年度与缴别
								if ("0".equals(fiItemTrans.getFiPolicyYear()) && "0".equals(fiItemTrans.getFiPayIntv())) {
									fiDataTransDetail.setFiPolicyYear("0");
									fiDataTransDetail.setFiPayIntv("0");
								} else {
									//计算记账时间与保单生效日之间的年份差
									Calendar cvaliDate = Calendar.getInstance();
									System.out.println(fiDataTransDetail.getCvaliDate());
									cvaliDate.setTime(fiDataTransDetail.getCvaliDate());
									Calendar accountDate = Calendar.getInstance();
									accountDate.setTime(fiDataTransDetail.getAccountDate());

									int yearOfCvaliDate = cvaliDate.get(Calendar.YEAR);
									int yearOfAccountDate = accountDate.get(Calendar.YEAR);
									int monthOfCvaliDate = cvaliDate.get(Calendar.MONTH)+1;
									int monthOfAccountDate = accountDate.get(Calendar.MONTH)+1;
									int dayOfCvaliDate = cvaliDate.get(Calendar.DAY_OF_MONTH);
									int dayOfAccountDate = accountDate.get(Calendar.DAY_OF_MONTH);

									int yearInterval = yearOfAccountDate - yearOfCvaliDate + 1;
									if (monthOfCvaliDate > monthOfAccountDate || monthOfCvaliDate == monthOfAccountDate && dayOfAccountDate < dayOfCvaliDate) {
										yearInterval--;
									}

									int payintv = Integer.parseInt(fiDataTransDetail.getPayIntv());
									if (payintv == 0) {
										fiDataTransDetail.setFiPayIntv("2010");//趸交
									} else if (yearInterval == 1) {
										int monthInterval = monthOfAccountDate + 12 - monthOfCvaliDate;
										if (dayOfAccountDate < dayOfCvaliDate) {
											monthInterval--;
										}
										if (monthInterval < payintv) {
											fiDataTransDetail.setFiPayIntv("1010");//首年首期
										} else {
											fiDataTransDetail.setFiPayIntv("1011");//首年续期
										}
									} else {
										fiDataTransDetail.setFiPayIntv("1020");// 续年续期
									}

									String years = Integer.toString(yearInterval);
									if (1 == years.length()) {
										fiDataTransDetail.setFiPolicyYear("00" + years);
									} else if (2 == years.length()) {
										fiDataTransDetail.setFiPolicyYear("0" + years);
									} else {
										fiDataTransDetail.setFiPolicyYear(years);
									}
								}

								//成本中心转换
								if ("0".equals(fiItemTrans.getFiCostCenter())) {
									fiDataTransDetail.setFiCostCenter("0");
								} else {
									if (fiDataTransDetail.getFiSalechnl().startsWith("6")) {
										fiDataTransDetail.setFiCostCenter("110701");
									} else if ("2".equals(fiDataTransDetail.getListFlag())) {
										fiDataTransDetail.setFiCostCenter("110401");
									} else {
										if (fiDataTransDetail.getManageCom().length() == 8) {
											if (fiDataTransDetail.getManageCom() == "86450101") {
												fiDataTransDetail.setFiCostCenter("1186450101");
											} else if (fiDataTransDetail.getManageCom() == "86450102") {
												fiDataTransDetail.setFiCostCenter("1186450102");
											} else {
												fiDataTransDetail.setFiCostCenter("110202");
											}
										} else {
											if (fiDataTransDetail.getFiSalechnl().startsWith("1")) {
												fiDataTransDetail.setFiCostCenter("110301");
											} else if (fiDataTransDetail.getFiSalechnl().startsWith("2")) {
												fiDataTransDetail.setFiCostCenter("110401");
											} else if (fiDataTransDetail.getFiSalechnl().startsWith("3")) {
												fiDataTransDetail.setFiCostCenter("110501");
											} else if (fiDataTransDetail.getFiSalechnl().startsWith("5")) {
												fiDataTransDetail.setFiCostCenter("110801");
											}
										}
									}

								}

								//渠道转换值
								if ("0".equals(fiItemTrans.getFiSalechnl())) {
									fiDataTransDetail.setFiSalechnl("0");
								} else {
									//个险，网销渠道，微信出单
									if ("1".equals(fiDataTransDetail.getListFlag())
											&& fiDataTransDetail.getPolicySource().endsWith("16") && fiDataTransDetail.getFiSalechnl().startsWith("6")) {
										// 代理人编码agentcode=E0000001 代理机构agentcom not in (E45000001,8645) 代理银行为空  --->  转换值 0415
										if ("E0000001".equals(fiDataTransDetail.getAgentCode())
												&& !"E45000001".equals(fiDataTransDetail.getAgentCom()) && !"8645".equals(fiDataTransDetail.getAgentCom())
												&& "".equals(fiDataTransDetail.getAgentBankList())) {
											fiDataTransDetail.setFiSalechnl("0415");
										} else {
											fiDataTransDetail.setFiSalechnl("0413");
										}
									}else if("65".equals(fiDataTransDetail.getPolicySource())){
										fiDataTransDetail.setFiSalechnl("0414");
									}else if("203".equals(fiDataTransDetail.getFiSalechnl())){// 团险代理
										if("01".equals(fiDataTransDetail.getAgentCom())){
											fiDataTransDetail.setFiSalechnl("0207");
										}
										if ("02".equals(fiDataTransDetail.getAgentCom())){
											fiDataTransDetail.setFiSalechnl("0209");
										}
										if ("03".equals(fiDataTransDetail.getAgentCom())){
											fiDataTransDetail.setFiSalechnl("0208");
										}
									}else{
										fiDataTransDetail.setFiSalechnl(saleChnlMap.get(fiDataTransDetail.getFiSalechnl()));
									}
								}

								//现金流量段
								if ("0".equals(fiItemTrans.getFiCashFlowno())) {
									fiDataTransDetail.setFiCashFlowno("0");
								} else {
									String info = fiDataTransDetail.getFiCashFlowno();
									String fiCashFlowno = feeTypeMap.get(info);

									//新单、续期收费
									if("S,01".equals(info) || "S,02".equals(info)){
										if(capaccMap.get(fiDataTransDetail.getFiRiskCode()).startsWith("4") && "01".equals(fiDataTransDetail.getVersionNo())){
											fiCashFlowno = "C1106";
										}
									}

									//保全收费
									if ("S,03".equals(info)) {
										if ("LR".equals(fiDataTransDetail.getFeeType())) {
											fiCashFlowno = "C1150";
										} else if ("P008".equals(fiDataTransDetail.getFeeDetail()) || "P024".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C2107";
										} else if ("P009".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C2116";
										} else if ("RE".equals(fiDataTransDetail.getFeeType()) && "P015".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C1108";
										} else if ("WP".equals(fiDataTransDetail.getFeeType()) && "01".equals(fiDataTransDetail.getVersionNo())) {
											fiCashFlowno = "C1106";
										}
									}
									//保全付费
									if ("F,03".equals(info)) {
										if (capaccMap.get(fiDataTransDetail.getFiRiskCode()).startsWith("4") && "01".equals(fiDataTransDetail.getVersionNo())
												&& ("WT".equals(fiDataTransDetail.getFeeType()) || "CT".equals(fiDataTransDetail.getFeeType()) || "OP".equals(fiDataTransDetail.getFeeType()))) {
											fiCashFlowno = "C1214";
										} else if ("P008".equals(fiDataTransDetail.getFeeDetail()) || "P024".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C2107";
										} else if ("P009".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C2116";
										} else if ("LN".equals(fiDataTransDetail.getFeeType()) && "G009".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C2208";
										} else if ("YF".equals(fiDataTransDetail.getFeeFinaType()) && "G004".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C1204";
										} else if ("EF".equals(fiDataTransDetail.getFeeFinaType()) && "G004".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C1203";
										} else if ("30".equals(proYearMap.get(fiDataTransDetail.getFiRiskCode()))
												&& ("CT".equals(fiDataTransDetail.getFeeType()) || "XT".equals(fiDataTransDetail.getFeeType()) || "PT".equals(fiDataTransDetail.getFeeType()))) {
											fiCashFlowno = "C1212";
										}
									}
									//理赔付费
									if ("F,05".equals(info)) {
										if ("C04".equals(fiDataTransDetail.getFeeType()) && "YF".equals(fiDataTransDetail.getFeeFinaType())) {
											fiCashFlowno = "C1204";
										} else if ("C04".equals(fiDataTransDetail.getFeeType()) && "EF".equals(fiDataTransDetail.getFeeFinaType())) {
											fiCashFlowno = "C1203";
										} else if (!capaccMap.get(fiDataTransDetail.getFiRiskCode()).startsWith("4")
												&& ("C01".equals(fiDataTransDetail.getFeeType()) || "C02".equals(fiDataTransDetail.getFeeType()) || "D01".equals(fiDataTransDetail.getFeeType()))) {
											fiCashFlowno = "C1101";
										} else if (capaccMap.get(fiDataTransDetail.getFiRiskCode()).startsWith("4") && "D01".equals(fiDataTransDetail.getFeeType())) {
											fiCashFlowno = "C1212";
										} else if ("C03".equals(fiDataTransDetail.getFeeType()) && "C0301".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C2109";
										} else if ("C03".equals(fiDataTransDetail.getFeeType()) && "C0302".equals(fiDataTransDetail.getFeeDetail())) {
											fiCashFlowno = "C2116";
										} else if ("A".equals(fiDataTransDetail.getFeeType()) && !"30".equals(proYearMap.get(fiDataTransDetail.getFiRiskCode()))) {
											fiCashFlowno = "C1202";
										} else if ("B".equals(fiDataTransDetail.getFeeType()) && !capaccMap.get(fiDataTransDetail.getFiRiskCode()).startsWith("4") && "30".equals(proYearMap.get(fiDataTransDetail.getFiRiskCode()))) {
											fiCashFlowno = "C1202";
										} else if ("D02".equals(fiDataTransDetail.getFeeType())) {
											fiCashFlowno = "C1212";
										}
									}
									fiDataTransDetail.setFiCashFlowno(fiCashFlowno);
								}
								break;
							}

						}
						if(fiDataTransDetail.getFiManageCom()==null || fiDataTransDetail.getFiManageCom()==""){
							fiDataTransDetail.setFiManageCom("0");
						}
						if(fiDataTransDetail.getFiCostCenter()==null || fiDataTransDetail.getFiCostCenter()==""){
							fiDataTransDetail.setFiCostCenter("0");
						}
						if(fiDataTransDetail.getFiBankCode()==null || fiDataTransDetail.getFiBankCode()==""){
							fiDataTransDetail.setFiBankCode("0");
						}
						if(fiDataTransDetail.getFiExecuteCom()==null || fiDataTransDetail.getFiExecuteCom()==""){
							fiDataTransDetail.setFiExecuteCom("0");
						}
						if(fiDataTransDetail.getFiRiskCode()==null || fiDataTransDetail.getFiRiskCode()==""){
							fiDataTransDetail.setFiRiskCode("0");
						}
						if(fiDataTransDetail.getFiSalechnl()==null || fiDataTransDetail.getFiSalechnl()==""){
							fiDataTransDetail.setFiSalechnl("0");
						}
						if(fiDataTransDetail.getFiProyear()==null || fiDataTransDetail.getFiProyear()==""){
							fiDataTransDetail.setFiProyear("0");
						}
						if(fiDataTransDetail.getFiPayYears()==null || fiDataTransDetail.getFiPayYears()==""){
							fiDataTransDetail.setFiPayYears("0");
						}
						if(fiDataTransDetail.getFiPolicyYear()==null || fiDataTransDetail.getFiPolicyYear()==""){
							fiDataTransDetail.setFiPolicyYear("0");
						}
						if(fiDataTransDetail.getFiRateFlag()==null || fiDataTransDetail.getFiRateFlag()==""){
							fiDataTransDetail.setFiRateFlag("0");
						}
						if(fiDataTransDetail.getFiPayIntv()==null || fiDataTransDetail.getFiPayIntv()==""){
							fiDataTransDetail.setFiPayIntv("0");
						}
						if(fiDataTransDetail.getFiCashFlowno()==null || fiDataTransDetail.getFiCashFlowno()==""){
							fiDataTransDetail.setFiCashFlowno("0");
						}
						if(fiDataTransDetail.getFiCapacc()==null || fiDataTransDetail.getFiCapacc()==""){
							fiDataTransDetail.setFiCapacc("0");
						}
						if(fiDataTransDetail.getFiPayMode()==null || fiDataTransDetail.getFiPayMode()==""){
							fiDataTransDetail.setFiPayMode("0");
						}
						if(fiDataTransDetail.getFiCashSalechnl()==null || fiDataTransDetail.getFiCashSalechnl()==""){
							fiDataTransDetail.setFiCashSalechnl("0");
						}
						fiDataTransDetail.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
						fiDataTransDetail.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
						fiDataTransDetailService.saveDataTransDetail(fiDataTransDetail);
					}

				} else if ("02".equals(rule.getCheckType())) {// 其他
					List<FiDataTransDetail> fiDataTransDetailDatas = fiDataTransDetailService.queryData(mainSQL);
					for (int j = 0; j < fiDataTransDetailDatas.size(); j++) {
						fiDataTransDetailDatas.get(j).setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
						fiDataTransDetailDatas.get(j).setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
						fiDataTransDetailService.saveDataTransDetail(fiDataTransDetailDatas.get(j));
					}
				}
			}

			//删除备份表中的数并将数据差异表的数插入备份表中,再删除数据差异表数据
			fiDataCheckDetailBakService.deleteFromFiDataCkeckDetailBak(coreSearch);
			List<FiDataCheckDetailBak> fiDataCheckDetailBaks = fiDataCheckDetailBakService.getFiDataCheckDetailDatas(coreSearch);
			for (int i = 0; i < fiDataCheckDetailBaks.size(); i++) {
				fiDataCheckDetailBakService.saveFiDataCheckDetailBak(fiDataCheckDetailBaks.get(i));
			}
			fiDataCheckDetailService.deleteFromFiDataCkeckDetail(coreSearch);

			//取managecom转换前后的值以键值对形式存储
			List<FiCodeTrans> manageComList=fiCodeTransService.getSixManageComList();
			Map<String,String> manageComMap=new HashMap<String,String>();
			for(int i=0;i<manageComList.size();i++){
				manageComMap.put(manageComList.get(i).getCodeAlias(),manageComList.get(i).getCode());
			}

			//按层级取核心凭证和财务凭证金额汇总数据
			//将要存储的数据，以集合形式存储
			List<FiDataCheckDetail> saveCheckDetailList=new ArrayList<FiDataCheckDetail>();
			List<FiDataCheckDetail> fiDataTransDetailsOfMoney = fiDataCheckDetailService.getFiDataTransDetailDatasOfMoney(coreSearch);
			List<FiDataCheckDetail> coreGlInterfacesOfMoney = fiDataCheckDetailService.getCoreGlInterfaceDatasOfMoney(coreSearch);
			jyList.add(new Date().toString() + "开始专项校验");
			// 业财/核心数据提取均为有数据
			if (fiDataTransDetailsOfMoney.size() > 0 && coreGlInterfacesOfMoney.size() > 0) {
				//开始核对金额，按照同记账准则、业务大类、机构、记账日期、科目和保单下的同一个保单下的金额进行核对
				for (int i = 0; i < fiDataTransDetailsOfMoney.size(); i++) {
					FiDataCheckDetail fiDataTransDetail = fiDataTransDetailsOfMoney.get(i);
					int transFlag=0;
					for (int j = 0; j < coreGlInterfacesOfMoney.size(); j++) {
						FiDataCheckDetail coreGlInterface = coreGlInterfacesOfMoney.get(j);
						// 比对金额差异(同一科目对应多个costid)
						if (fiDataTransDetail.getContNo().equals(coreGlInterface.getContNo()) &&
								fiDataTransDetail.getFiManageCom().equals(coreGlInterface.getFiManageCom()) &&
								fiDataTransDetail.getFiMoney() != coreGlInterface.getCoreMoney() &&
								fiDataTransDetail.getVersionNo().equals(coreGlInterface.getVersionNo()) &&
								fiDataTransDetail.getAccountCode().equals(coreGlInterface.getAccountCode()) &&
								fiDataTransDetail.getBusType().equals(coreGlInterface.getBusType()) &&
								fiDataTransDetail.getCheckType().equals(coreGlInterface.getCheckType()) &&
								fiDataTransDetail.getAccountDate().toString().equals(coreGlInterface.getAccountDate().toString()) &&
								fiDataTransDetail.getFinItemType().equals(coreGlInterface.getFinItemType()) &&
								fiDataTransDetail.getFiCostCenter().equals(coreGlInterface.getFiCostCenter()) &&
								fiDataTransDetail.getFiBankCode().equals(coreGlInterface.getFiBankCode()) &&
								fiDataTransDetail.getFiExecuteCom().equals(coreGlInterface.getFiExecuteCom()) &&
								fiDataTransDetail.getFiRiskCode().equals(coreGlInterface.getFiRiskCode()) &&
								fiDataTransDetail.getFiSalechnl().equals(coreGlInterface.getFiSalechnl()) &&
								fiDataTransDetail.getFiProyear().equals(coreGlInterface.getFiProyear()) &&
								fiDataTransDetail.getFiPayYears().equals(coreGlInterface.getFiPayYears()) &&
								fiDataTransDetail.getFiPolicyYear().equals(coreGlInterface.getFiPolicyYear()) &&
								fiDataTransDetail.getFiRateFlag().equals(coreGlInterface.getFiRateFlag()) &&
								fiDataTransDetail.getFiPayIntv().equals(coreGlInterface.getFiPayIntv()) &&
								fiDataTransDetail.getFiCashFlowNo().equals(coreGlInterface.getFiCashFlowNo()) &&
								fiDataTransDetail.getFiCapacc().equals(coreGlInterface.getFiCapacc())) {
								fiDataTransDetail.setFiCostCenter("");
								fiDataTransDetail.setFiBankCode("");
								fiDataTransDetail.setFiExecuteCom("");
								fiDataTransDetail.setFiRiskCode("");
								fiDataTransDetail.setFiSalechnl("");
								fiDataTransDetail.setFiProyear("");
								fiDataTransDetail.setFiPayYears("");
								fiDataTransDetail.setFiPolicyYear("");
								fiDataTransDetail.setFiRateFlag("");
								fiDataTransDetail.setFiPayIntv("");
								fiDataTransDetail.setFiCashFlowNo("");
								fiDataTransDetail.setFiCapacc("");
								fiDataTransDetail.setManageCom(manageComMap.get(fiDataTransDetail.getFiManageCom()));
								fiDataTransDetail.setCheckMoney(coreGlInterface.getCoreMoney() - fiDataTransDetail.getFiMoney());
								fiDataTransDetail.setCoreMoney(coreGlInterface.getCoreMoney());
								fiDataTransDetail.setErrorType("01"); // 金额差异0
								fiDataTransDetail.setErrorInfo("仅存在金额差异");
								fiDataTransDetail.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
								fiDataTransDetail.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
								saveCheckDetailList.add(fiDataTransDetail);
								transFlag=1;
								break;
						}else if (fiDataTransDetail.getContNo().equals(coreGlInterface.getContNo()) &&
								fiDataTransDetail.getFiManageCom().equals(coreGlInterface.getFiManageCom()) &&
								fiDataTransDetail.getFiMoney() == coreGlInterface.getCoreMoney() &&
								fiDataTransDetail.getVersionNo().equals(coreGlInterface.getVersionNo()) &&
								fiDataTransDetail.getAccountCode().equals(coreGlInterface.getAccountCode()) &&
								fiDataTransDetail.getBusType().equals(coreGlInterface.getBusType()) &&
								fiDataTransDetail.getCheckType().equals(coreGlInterface.getCheckType()) &&
								fiDataTransDetail.getAccountDate().toString().equals(coreGlInterface.getAccountDate().toString()) &&
								fiDataTransDetail.getFinItemType().equals(coreGlInterface.getFinItemType()) &&
								fiDataTransDetail.getFiCostCenter().equals(coreGlInterface.getFiCostCenter()) &&
								fiDataTransDetail.getFiBankCode().equals(coreGlInterface.getFiBankCode()) &&
								fiDataTransDetail.getFiExecuteCom().equals(coreGlInterface.getFiExecuteCom()) &&
								fiDataTransDetail.getFiRiskCode().equals(coreGlInterface.getFiRiskCode()) &&
								fiDataTransDetail.getFiSalechnl().equals(coreGlInterface.getFiSalechnl()) &&
								fiDataTransDetail.getFiProyear().equals(coreGlInterface.getFiProyear()) &&
								fiDataTransDetail.getFiPayYears().equals(coreGlInterface.getFiPayYears()) &&
								fiDataTransDetail.getFiPolicyYear().equals(coreGlInterface.getFiPolicyYear()) &&
								fiDataTransDetail.getFiRateFlag().equals(coreGlInterface.getFiRateFlag()) &&
								fiDataTransDetail.getFiPayIntv().equals(coreGlInterface.getFiPayIntv()) &&
								fiDataTransDetail.getFiCashFlowNo().equals(coreGlInterface.getFiCashFlowNo()) &&
								fiDataTransDetail.getFiCapacc().equals(coreGlInterface.getFiCapacc())) {
							//核心与业财记账结果相同，checktype标记00，为对账成功数据，便于计算机构差异
							fiDataTransDetail.setFiCostCenter("");
							fiDataTransDetail.setFiBankCode("");
							fiDataTransDetail.setFiExecuteCom("");
							fiDataTransDetail.setFiRiskCode("");
							fiDataTransDetail.setFiSalechnl("");
							fiDataTransDetail.setFiProyear("");
							fiDataTransDetail.setFiPayYears("");
							fiDataTransDetail.setFiPolicyYear("");
							fiDataTransDetail.setFiRateFlag("");
							fiDataTransDetail.setFiPayIntv("");
							fiDataTransDetail.setFiCashFlowNo("");
							fiDataTransDetail.setFiCapacc("");
							fiDataTransDetail.setManageCom(manageComMap.get(fiDataTransDetail.getFiManageCom()));
							fiDataTransDetail.setCheckMoney(coreGlInterface.getCoreMoney()-fiDataTransDetail.getFiMoney());
							fiDataTransDetail.setCoreMoney(coreGlInterface.getCoreMoney());
							fiDataTransDetail.setErrorType("00"); // 保单对账成功
							fiDataTransDetail.setErrorInfo("对账成功，无差异");
							fiDataTransDetail.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
							fiDataTransDetail.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
							saveCheckDetailList.add(fiDataTransDetail);
							transFlag=2;
							break;
						}else if (fiDataTransDetail.getContNo().equals(coreGlInterface.getContNo()) &&
								fiDataTransDetail.getFiManageCom().equals(coreGlInterface.getFiManageCom()) &&
								fiDataTransDetail.getVersionNo().equals(coreGlInterface.getVersionNo()) &&
								fiDataTransDetail.getAccountCode().equals(coreGlInterface.getAccountCode()) &&
								fiDataTransDetail.getBusType().equals(coreGlInterface.getBusType()) &&
								fiDataTransDetail.getCheckType().equals(coreGlInterface.getCheckType()) &&
								fiDataTransDetail.getAccountDate().toString().equals(coreGlInterface.getAccountDate().toString()) &&
								fiDataTransDetail.getFinItemType().equals(coreGlInterface.getFinItemType()) &&
								fiDataTransDetail.getFiRiskCode().equals(coreGlInterface.getFiRiskCode())){
								//险种一致情况下的保单核对
									if(!fiDataTransDetail.getFiCostCenter().equals(coreGlInterface.getFiCostCenter())){
										fiDataTransDetail.setFiCostCenter("成本中心段错误;");
									}else{
										fiDataTransDetail.setFiCostCenter("");
									}
									if(!fiDataTransDetail.getFiBankCode().equals(coreGlInterface.getFiBankCode())){
										fiDataTransDetail.setFiBankCode("银行段错误;");
									}else{
										fiDataTransDetail.setFiBankCode("");
									}
									if(!fiDataTransDetail.getFiExecuteCom().equals(coreGlInterface.getFiExecuteCom())){
										fiDataTransDetail.setFiExecuteCom("异地段错误;");
									}else{
										fiDataTransDetail.setFiExecuteCom("");
									}
									if(!fiDataTransDetail.getFiSalechnl().equals(coreGlInterface.getFiSalechnl())){
										fiDataTransDetail.setFiSalechnl("渠道段错误;");
									}else{
										fiDataTransDetail.setFiSalechnl("");
									}
									if(!fiDataTransDetail.getFiProyear().equals(coreGlInterface.getFiProyear())){
										fiDataTransDetail.setFiProyear("产品期限错误;");
									}else{
										fiDataTransDetail.setFiProyear("");
									}
									if(!fiDataTransDetail.getFiPayYears().equals(coreGlInterface.getFiPayYears())){
										fiDataTransDetail.setFiPayYears("缴费期限错误;");
									}else{
										fiDataTransDetail.setFiPayYears("");
									}
									if(!fiDataTransDetail.getFiPolicyYear().equals(coreGlInterface.getFiPolicyYear())){
										fiDataTransDetail.setFiPolicyYear("保单年度错误;");
									}else{
										fiDataTransDetail.setFiPolicyYear("");
									}
									if(!fiDataTransDetail.getFiRateFlag().equals(coreGlInterface.getFiRateFlag())){
										fiDataTransDetail.setFiRateFlag("应免税标识错误;");
									}else{
										fiDataTransDetail.setFiRateFlag("");
									}
									if(!fiDataTransDetail.getFiPayIntv().equals(coreGlInterface.getFiPayIntv())){
										fiDataTransDetail.setFiPayIntv("缴别错误;");
									}else{
										fiDataTransDetail.setFiPayIntv("");
									}
									if(!fiDataTransDetail.getFiCashFlowNo().equals(coreGlInterface.getFiCashFlowNo())){
										fiDataTransDetail.setFiCashFlowNo("预算及现金流错误;");
									}else{
										fiDataTransDetail.setFiCashFlowNo("");
									}
									if(!fiDataTransDetail.getFiCapacc().equals(coreGlInterface.getFiCapacc())){
										fiDataTransDetail.setFiCapacc("资金账户错误;");
									}else{
										fiDataTransDetail.setFiCapacc("");
									}
									if(fiDataTransDetail.getFiMoney() == coreGlInterface.getCoreMoney()){
										fiDataTransDetail.setErrorType("04");
										fiDataTransDetail.setErrorInfo("专项存在差异");
									}else{
										fiDataTransDetail.setErrorType("05");
										fiDataTransDetail.setErrorInfo("金额及专项均存在差异");
									}
								fiDataTransDetail.setFiRiskCode("");
								//专项差异
								fiDataTransDetail.setManageCom(manageComMap.get(fiDataTransDetail.getFiManageCom()));
								fiDataTransDetail.setCheckMoney(coreGlInterface.getCoreMoney()-fiDataTransDetail.getFiMoney());
								fiDataTransDetail.setCoreMoney(coreGlInterface.getCoreMoney());
								fiDataTransDetail.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
								fiDataTransDetail.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
								saveCheckDetailList.add(fiDataTransDetail);
								transFlag=3;
								break;
						}else if (fiDataTransDetail.getContNo().equals(coreGlInterface.getContNo()) &&
								fiDataTransDetail.getFiManageCom().equals(coreGlInterface.getFiManageCom()) &&
								fiDataTransDetail.getVersionNo().equals(coreGlInterface.getVersionNo()) &&
								fiDataTransDetail.getAccountCode().equals(coreGlInterface.getAccountCode()) &&
								fiDataTransDetail.getBusType().equals(coreGlInterface.getBusType()) &&
								fiDataTransDetail.getCheckType().equals(coreGlInterface.getCheckType()) &&
								fiDataTransDetail.getAccountDate().toString().equals(coreGlInterface.getAccountDate().toString()) &&
								fiDataTransDetail.getFinItemType().equals(coreGlInterface.getFinItemType()) &&
								!fiDataTransDetail.getFiRiskCode().equals(coreGlInterface.getFiRiskCode())){
							//险种不一致情况下的保单核对
							fiDataTransDetail.setFiRiskCode("产品段错误;");
							if(!fiDataTransDetail.getFiCostCenter().equals(coreGlInterface.getFiCostCenter())){
								fiDataTransDetail.setFiCostCenter("成本中心段错误;");
							}else{
								fiDataTransDetail.setFiCostCenter("");
							}
							if(!fiDataTransDetail.getFiBankCode().equals(coreGlInterface.getFiBankCode())){
								fiDataTransDetail.setFiBankCode("银行段错误;");
							}else{
								fiDataTransDetail.setFiBankCode("");
							}
							if(!fiDataTransDetail.getFiExecuteCom().equals(coreGlInterface.getFiExecuteCom())){
								fiDataTransDetail.setFiExecuteCom("异地段错误;");
							}else{
								fiDataTransDetail.setFiExecuteCom("");
							}
							if(!fiDataTransDetail.getFiSalechnl().equals(coreGlInterface.getFiSalechnl())){
								fiDataTransDetail.setFiSalechnl("渠道段错误;");
							}else{
								fiDataTransDetail.setFiSalechnl("");
							}
							if(!fiDataTransDetail.getFiProyear().equals(coreGlInterface.getFiProyear())){
								fiDataTransDetail.setFiProyear("产品期限错误;");
							}else{
								fiDataTransDetail.setFiProyear("");
							}
							if(!fiDataTransDetail.getFiPayYears().equals(coreGlInterface.getFiPayYears())){
								fiDataTransDetail.setFiPayYears("缴费期限错误;");
							}else{
								fiDataTransDetail.setFiPayYears("");
							}
							if(!fiDataTransDetail.getFiPolicyYear().equals(coreGlInterface.getFiPolicyYear())){
								fiDataTransDetail.setFiPolicyYear("保单年度错误;");
							}else{
								fiDataTransDetail.setFiPolicyYear("");
							}
							if(!fiDataTransDetail.getFiRateFlag().equals(coreGlInterface.getFiRateFlag())){
								fiDataTransDetail.setFiRateFlag("应免税标识错误;");
							}else{
								fiDataTransDetail.setFiRateFlag("");
							}
							if(!fiDataTransDetail.getFiPayIntv().equals(coreGlInterface.getFiPayIntv())){
								fiDataTransDetail.setFiPayIntv("缴别错误;");
							}else{
								fiDataTransDetail.setFiPayIntv("");
							}
							if(!fiDataTransDetail.getFiCashFlowNo().equals(coreGlInterface.getFiCashFlowNo())){
								fiDataTransDetail.setFiCashFlowNo("预算及现金流错误;");
							}else{
								fiDataTransDetail.setFiCashFlowNo("");
							}
							if(!fiDataTransDetail.getFiCapacc().equals(coreGlInterface.getFiCapacc())){
								fiDataTransDetail.setFiCapacc("资金账户错误;");
							}else{
								fiDataTransDetail.setFiCapacc("");
							}
							if(fiDataTransDetail.getFiMoney() == coreGlInterface.getCoreMoney()){
								fiDataTransDetail.setErrorType("04");
								fiDataTransDetail.setErrorInfo("专项存在差异");
							}else{
								fiDataTransDetail.setErrorType("05");
								fiDataTransDetail.setErrorInfo("金额及专项均存在差异");
							}
							//专项差异
							fiDataTransDetail.setManageCom(manageComMap.get(fiDataTransDetail.getFiManageCom()));
							fiDataTransDetail.setCheckMoney(coreGlInterface.getCoreMoney()-fiDataTransDetail.getFiMoney());
							fiDataTransDetail.setCoreMoney(coreGlInterface.getCoreMoney());
							fiDataTransDetail.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
							fiDataTransDetail.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
							saveCheckDetailList.add(fiDataTransDetail);
							transFlag=4;
							break;
						}
					}
                    // 其他差异情况暂时都保留这里（如：机构差异、记账日期差异）
                    if (transFlag==0) {
						fiDataTransDetail.setFiCostCenter("");
						fiDataTransDetail.setFiBankCode("");
						fiDataTransDetail.setFiExecuteCom("");
						fiDataTransDetail.setFiRiskCode("");
						fiDataTransDetail.setFiSalechnl("");
						fiDataTransDetail.setFiProyear("");
						fiDataTransDetail.setFiPayYears("");
						fiDataTransDetail.setFiPolicyYear("");
						fiDataTransDetail.setFiRateFlag("");
						fiDataTransDetail.setFiPayIntv("");
						fiDataTransDetail.setFiCashFlowNo("");
						fiDataTransDetail.setFiCapacc("");
						fiDataTransDetail.setManageCom(manageComMap.get(fiDataTransDetail.getFiManageCom()));
                        fiDataTransDetail.setCheckMoney(0-fiDataTransDetail.getFiMoney());
                        fiDataTransDetail.setCoreMoney(0);
                        fiDataTransDetail.setErrorType("02"); // 未记账
                        fiDataTransDetail.setErrorInfo("未记账，无财务数据");
                        fiDataTransDetail.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
                        fiDataTransDetail.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
                        saveCheckDetailList.add(fiDataTransDetail);
                    }
				}
				// 疑似新业务，业财未提取或是记账错误（核心有数据，业财无数据）
				for (int i = 0; i < coreGlInterfacesOfMoney.size(); i++) {
					FiDataCheckDetail coreGlInterface = coreGlInterfacesOfMoney.get(i);
					int coreFlag=0;
					for (int j = 0; j < fiDataTransDetailsOfMoney.size(); j++) {
						FiDataCheckDetail fiDataTransDetail = fiDataTransDetailsOfMoney.get(j);
						// 比对金额差异，上面已判断，此处直接略过
						if (fiDataTransDetail.getContNo().equals(coreGlInterface.getContNo()) &&
								fiDataTransDetail.getFiManageCom().equals(coreGlInterface.getFiManageCom()) &&
								fiDataTransDetail.getFiMoney() != coreGlInterface.getCoreMoney() &&
								fiDataTransDetail.getVersionNo().equals(coreGlInterface.getVersionNo()) &&
								fiDataTransDetail.getAccountCode().equals(coreGlInterface.getAccountCode()) &&
								fiDataTransDetail.getBusType().equals(coreGlInterface.getBusType()) &&
								fiDataTransDetail.getCheckType().equals(coreGlInterface.getCheckType()) &&
								fiDataTransDetail.getAccountDate().toString().equals(coreGlInterface.getAccountDate().toString()) &&
								fiDataTransDetail.getFinItemType().equals(coreGlInterface.getFinItemType()) &&
								fiDataTransDetail.getFiCostCenter().equals(coreGlInterface.getFiCostCenter()) &&
								fiDataTransDetail.getFiBankCode().equals(coreGlInterface.getFiBankCode()) &&
								fiDataTransDetail.getFiExecuteCom().equals(coreGlInterface.getFiExecuteCom()) &&
								fiDataTransDetail.getFiRiskCode().equals(coreGlInterface.getFiRiskCode()) &&
								fiDataTransDetail.getFiSalechnl().equals(coreGlInterface.getFiSalechnl()) &&
								fiDataTransDetail.getFiProyear().equals(coreGlInterface.getFiProyear()) &&
								fiDataTransDetail.getFiPayYears().equals(coreGlInterface.getFiPayYears()) &&
								fiDataTransDetail.getFiPolicyYear().equals(coreGlInterface.getFiPolicyYear()) &&
								fiDataTransDetail.getFiRateFlag().equals(coreGlInterface.getFiRateFlag()) &&
								fiDataTransDetail.getFiPayIntv().equals(coreGlInterface.getFiPayIntv()) &&
								fiDataTransDetail.getFiCashFlowNo().equals(coreGlInterface.getFiCashFlowNo()) &&
								fiDataTransDetail.getFiCapacc().equals(coreGlInterface.getFiCapacc())) {
							coreFlag=1;
							break;
						}else if (fiDataTransDetail.getContNo().equals(coreGlInterface.getContNo()) &&
								fiDataTransDetail.getFiManageCom().equals(coreGlInterface.getFiManageCom()) &&
								fiDataTransDetail.getFiMoney() == coreGlInterface.getCoreMoney() &&
								fiDataTransDetail.getVersionNo().equals(coreGlInterface.getVersionNo()) &&
								fiDataTransDetail.getAccountCode().equals(coreGlInterface.getAccountCode()) &&
								fiDataTransDetail.getBusType().equals(coreGlInterface.getBusType()) &&
								fiDataTransDetail.getCheckType().equals(coreGlInterface.getCheckType()) &&
								fiDataTransDetail.getAccountDate().toString().equals(coreGlInterface.getAccountDate().toString()) &&
								fiDataTransDetail.getFinItemType().equals(coreGlInterface.getFinItemType()) &&
								fiDataTransDetail.getFiCostCenter().equals(coreGlInterface.getFiCostCenter()) &&
								fiDataTransDetail.getFiBankCode().equals(coreGlInterface.getFiBankCode()) &&
								fiDataTransDetail.getFiExecuteCom().equals(coreGlInterface.getFiExecuteCom()) &&
								fiDataTransDetail.getFiRiskCode().equals(coreGlInterface.getFiRiskCode()) &&
								fiDataTransDetail.getFiSalechnl().equals(coreGlInterface.getFiSalechnl()) &&
								fiDataTransDetail.getFiProyear().equals(coreGlInterface.getFiProyear()) &&
								fiDataTransDetail.getFiPayYears().equals(coreGlInterface.getFiPayYears()) &&
								fiDataTransDetail.getFiPolicyYear().equals(coreGlInterface.getFiPolicyYear()) &&
								fiDataTransDetail.getFiRateFlag().equals(coreGlInterface.getFiRateFlag()) &&
								fiDataTransDetail.getFiPayIntv().equals(coreGlInterface.getFiPayIntv()) &&
								fiDataTransDetail.getFiCashFlowNo().equals(coreGlInterface.getFiCashFlowNo()) &&
								fiDataTransDetail.getFiCapacc().equals(coreGlInterface.getFiCapacc())) {
							//核心与业财记账结果相同，checktype标记00，为对账成功数据，便于计算机构差异
							coreFlag=2;
							break;
						}else if (fiDataTransDetail.getContNo().equals(coreGlInterface.getContNo()) &&
								fiDataTransDetail.getFiManageCom().equals(coreGlInterface.getFiManageCom()) &&
								fiDataTransDetail.getVersionNo().equals(coreGlInterface.getVersionNo()) &&
								fiDataTransDetail.getAccountCode().equals(coreGlInterface.getAccountCode()) &&
								fiDataTransDetail.getBusType().equals(coreGlInterface.getBusType()) &&
								fiDataTransDetail.getCheckType().equals(coreGlInterface.getCheckType()) &&
								fiDataTransDetail.getAccountDate().toString().equals(coreGlInterface.getAccountDate().toString()) &&
								fiDataTransDetail.getFinItemType().equals(coreGlInterface.getFinItemType()) &&
								fiDataTransDetail.getFiRiskCode().equals(coreGlInterface.getFiRiskCode())) {
							coreFlag=3;
							break;
						}else if(fiDataTransDetail.getContNo().equals(coreGlInterface.getContNo()) &&
								fiDataTransDetail.getFiManageCom().equals(coreGlInterface.getFiManageCom()) &&
								fiDataTransDetail.getVersionNo().equals(coreGlInterface.getVersionNo()) &&
								fiDataTransDetail.getAccountCode().equals(coreGlInterface.getAccountCode()) &&
								fiDataTransDetail.getBusType().equals(coreGlInterface.getBusType()) &&
								fiDataTransDetail.getCheckType().equals(coreGlInterface.getCheckType()) &&
								fiDataTransDetail.getAccountDate().toString().equals(coreGlInterface.getAccountDate().toString()) &&
								fiDataTransDetail.getFinItemType().equals(coreGlInterface.getFinItemType()) &&
								!fiDataTransDetail.getFiRiskCode().equals(coreGlInterface.getFiRiskCode())){
							coreFlag=4;
							break;
						}
					}
                    // 其他差异情况暂时都保留这里（如：机构差异、记账日期差异）
                    if (coreFlag==0) {
						coreGlInterface.setFiCostCenter("");
						coreGlInterface.setFiBankCode("");
						coreGlInterface.setFiExecuteCom("");
						coreGlInterface.setFiRiskCode("");
						coreGlInterface.setFiSalechnl("");
						coreGlInterface.setFiProyear("");
						coreGlInterface.setFiPayYears("");
						coreGlInterface.setFiPolicyYear("");
						coreGlInterface.setFiRateFlag("");
						coreGlInterface.setFiPayIntv("");
						coreGlInterface.setFiCashFlowNo("");
						coreGlInterface.setFiCapacc("");
						coreGlInterface.setManageCom(manageComMap.get(coreGlInterface.getFiManageCom()));
                        coreGlInterface.setCheckMoney(coreGlInterface.getCoreMoney()-0);
                        coreGlInterface.setFiMoney(0);
                        coreGlInterface.setErrorType("03"); // 未记账
                        coreGlInterface.setErrorInfo("记账错误，无相关业务数据");
                        coreGlInterface.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
                        coreGlInterface.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
                        saveCheckDetailList.add(coreGlInterface);
                    }
				}
			}
			// 业务数据提取无数据（有财务数据，无业务数据)
			if (fiDataTransDetailsOfMoney.size() == 0 && coreGlInterfacesOfMoney.size() != 0) {
				for (int j = 0; j < coreGlInterfacesOfMoney.size(); j++) {
					FiDataCheckDetail coreGlInterface = coreGlInterfacesOfMoney.get(j);
					coreGlInterface.setFiCostCenter("");
					coreGlInterface.setFiBankCode("");
					coreGlInterface.setFiExecuteCom("");
					coreGlInterface.setFiRiskCode("");
					coreGlInterface.setFiSalechnl("");
					coreGlInterface.setFiProyear("");
					coreGlInterface.setFiPayYears("");
					coreGlInterface.setFiPolicyYear("");
					coreGlInterface.setFiRateFlag("");
					coreGlInterface.setFiPayIntv("");
					coreGlInterface.setFiCashFlowNo("");
					coreGlInterface.setFiCapacc("");
					coreGlInterface.setManageCom(manageComMap.get(coreGlInterface.getFiManageCom()));
					coreGlInterface.setCheckMoney(coreGlInterface.getCoreMoney());
					coreGlInterface.setFiMoney(0);
					coreGlInterface.setErrorType("03"); // 无业务数据
					coreGlInterface.setErrorInfo("记账错误，无相关业务数据");
					coreGlInterface.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
					coreGlInterface.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
					saveCheckDetailList.add(coreGlInterface);
				}
			}
			//财务记账提取无数据(有业务数据，无财务数据)
			if (fiDataTransDetailsOfMoney.size() != 0 && coreGlInterfacesOfMoney.size() == 0) {
				for (int j = 0; j < fiDataTransDetailsOfMoney.size(); j++) {
					FiDataCheckDetail fiDataTransDetail = fiDataTransDetailsOfMoney.get(j);
					fiDataTransDetail.setFiCostCenter("");
					fiDataTransDetail.setFiBankCode("");
					fiDataTransDetail.setFiExecuteCom("");
					fiDataTransDetail.setFiRiskCode("");
					fiDataTransDetail.setFiSalechnl("");
					fiDataTransDetail.setFiProyear("");
					fiDataTransDetail.setFiPayYears("");
					fiDataTransDetail.setFiPolicyYear("");
					fiDataTransDetail.setFiRateFlag("");
					fiDataTransDetail.setFiPayIntv("");
					fiDataTransDetail.setFiCashFlowNo("");
					fiDataTransDetail.setFiCapacc("");
					fiDataTransDetail.setManageCom(manageComMap.get(fiDataTransDetail.getFiManageCom()));
					fiDataTransDetail.setCheckMoney(0-fiDataTransDetail.getFiMoney());
					fiDataTransDetail.setCoreMoney(0);
					fiDataTransDetail.setErrorType("02"); // 无财务数据
					fiDataTransDetail.setErrorInfo("未记账，无财务数据");
					fiDataTransDetail.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
					fiDataTransDetail.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
					saveCheckDetailList.add(fiDataTransDetail);
				}
			}
			//将差异结果存储至表
			for(int j=0;j<saveCheckDetailList.size();j++){
				jyList.add("contno为"+ saveCheckDetailList.get(j).getContNo()+"的数据存在异常,异常信息为"+saveCheckDetailList.get(j).getErrorInfo());
				fiDataCheckDetailService.saveFiDataCheckDetail(saveCheckDetailList.get(j));
			}
			jyList.add("共"+saveCheckDetailList.size()+"条差异数据,请查看数据库");

			// 科目汇总（科目汇总清单/机构汇总清单数据准备）
			// 因财务接口表，机构段到6位机构，成本中心段与机构无太大关联，汇总校验只能汇总到6位机构
			fiFinItemCheckDetailService.deleteFiFinItemCheckDetail(coreSearch);
			//按照前台传递条件查询机构汇总信息并插入至表
			List <FiFinItemCheckDetail> saveFiFinDetailList=new ArrayList<FiFinItemCheckDetail>();
			List<FiFinItemCheckDetail> datas = fiFinItemCheckDetailService.queryFiFinDataByTrans(coreSearch);
			//查询核心凭证下的总条数
			List<FiFinItemCheckDetail> coreLists = fiFinItemCheckDetailService.queryCoreToFiFinBySegment1Sum(coreSearch);
			//查询业务结果下的总条数
			List<FiFinItemCheckDetail> transLists = fiFinItemCheckDetailService.queryTransToFiFinBySegment1Sum(coreSearch);
			FiFinItemCheckDetail fiFinItemCheckDetail=new FiFinItemCheckDetail();
			//给FiFinItemCheckDetail表添加业务笔数与财务笔数，并储存数据
			for(int j=0;j<datas.size();j++){
				fiFinItemCheckDetail=datas.get(j);
				for(int k=0;k<coreLists.size();k++){
					if(fiFinItemCheckDetail.getAccountDate().equals(coreLists.get(k).getAccountDate())
						&& fiFinItemCheckDetail.getFiManageCom().equals(coreLists.get(k).getFiManageCom())
						&& fiFinItemCheckDetail.getVersionNo().equals(coreLists.get(k).getVersionNo())
						&& fiFinItemCheckDetail.getBusType().equals(coreLists.get(k).getBusType())
						&& fiFinItemCheckDetail.getCheckType().equals(coreLists.get(k).getCheckType())
						&& fiFinItemCheckDetail.getFinItemType().equals(coreLists.get(k).getFinItemType())
						&& fiFinItemCheckDetail.getAccountCode().equals(coreLists.get(k).getAccountCode())){
						fiFinItemCheckDetail.setcActuCount(coreLists.get(k).getcActuCount());
						break;
					}
				}
				for(int m=0;m<transLists.size();m++){
					if(fiFinItemCheckDetail.getAccountDate().equals(transLists.get(m).getAccountDate())
							&& fiFinItemCheckDetail.getFiManageCom().equals(transLists.get(m).getFiManageCom())
							&& fiFinItemCheckDetail.getVersionNo().equals(transLists.get(m).getVersionNo())
							&& fiFinItemCheckDetail.getBusType().equals(transLists.get(m).getBusType())
							&& fiFinItemCheckDetail.getCheckType().equals(transLists.get(m).getCheckType())
							&& fiFinItemCheckDetail.getFinItemType().equals(transLists.get(m).getFinItemType())
							&& fiFinItemCheckDetail.getAccountCode().equals(transLists.get(m).getAccountCode())){
						fiFinItemCheckDetail.setfCount(transLists.get(m).getfCount());
						break;
					}
				}

				fiFinItemCheckDetail.setManageCom(manageComMap.get(fiFinItemCheckDetail.getFiManageCom()));
				fiFinItemCheckDetail.setDifCount(Math.abs(fiFinItemCheckDetail.getcActuCount()-fiFinItemCheckDetail.getfCount()));
				fiFinItemCheckDetail.setMakeDate(sdf.parse(DateFormat.getDateInstance().format(new Date())));
				fiFinItemCheckDetail.setMakeTime(new java.sql.Time((sdfTime.parse(sdfTime.format(new Date()))).getTime()));
				saveFiFinDetailList.add(fiFinItemCheckDetail);
			}
			for(int i=0;i<saveFiFinDetailList.size();i++){
				fiFinItemCheckDetailService.saveDataTransDetail(saveFiFinDetailList.get(i));
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}
		inTxt();
		return JsonResult.success("");
	}

	private void inTxt() {
		log.info("正在写入TXT......");
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String property = System.getProperty("user.dir");
		String jyFile = property+"/专项段转换异常" ;
		File file = new File(jyFile);
		if (!file.exists()) {
			file.mkdir();
		}
		jyFile += "/"  + "专项段转换异常.txt";
		System.out.println("jyFile:" + jyFile);
		file = new File(jyFile);
//        if (!file.exists()) {
//            try {
//                file.createNewFile();
//            } catch (IOException e) {
//                log.info("创建文件" + jyFile + "时出错，异常为：" + e);
//                System.out.println(e);
//            }
//        }
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(new FileOutputStream(file, true));
			for (Object obj : jyList) {
				String str = (String) obj;
				pw.println(sdf.format(date) + ":" + str);
			}

			for (int i = 0; i < 2; i++) {
				pw.print("----------------------------------------------------------");
				pw.println("----------------------------------------------------------");
				pw.print("**********************************************************");
				pw.println("**********************************************************");
			}

		} catch (Exception e) {
			pw.close();
			log.info("PrintWriter流写入文件时出错,异常为：" + e);
			System.out.println(e);
		} finally {
			pw.close();
			for (Object obj : jyList) {
				String str = (String) obj;
				log.info(str);
			}
		}
	}




	public void test() {
		//if(科目=001){
		//    进科目表中查询001的配置；
		//    取到专项01的值；
		//    if(专项01的值为0){
		//        segment01缺省；
		//       }
		//    if(专项01的值为2){
		//        segment01取原值，不需要转；
		//		 }
		//    if(专项01的值为3){
		//        segment01按正常的转；
		//        进入专项表中根据专项查找到codeType；
		//        根据codeType去转换表中转换;
		//		 }
		//    if(专项01的值为4){
		//        segment01特殊处理；
		//        进入专项表中根据专项查找到对应的值；
		//        根据值判断对应的处理逻辑，在程序中处理;
		//       }
		// }


//		输入：业务大类/机构/记账准则/记账日期
//		根据业务大类和记账准则，获取记账规则集合 --业财数据库
//		循环规则表集合：
//			执行提数逻辑，获取结果集 --核心数据库
//				将业务数据结果集进行专项转换 --业财数据库
//		对财务结果集和业务数据处理的结果集进行比对，差异数据保存 --业财数据库


	}
	public static void main(String[] arg) {
		Map<String,String> map3 = new HashMap<>();
		String m = "m";
		String a = "a";
		String p = "p";
		map3.put(m+a,p);
		map3.put(m+a,p);

		System.out.println(map3.size());

//		Map<String,Double > m1 = new HashMap<>();
//		m1.put("contno1,riskcode1,2019-11-11",11.2);
//		m1.put("contno1,riskcode2,2019-11-11",10.0);
//		m1.put("contno2,riskcode1,2019-11-11",12.0);
//		m1.put("contno3,riskcode1,2019-11-11",11.2);
//		m1.put("contno3,riskcode2,2019-11-11",12.5);

//		Map<String,Double > m2 = new HashMap<>();
//		m2.put("contno1,riskcode1,2019-11-11",11.2);
//		m2.put("contno1,riskcode2,2019-11-11",10.0);
//		m2.put("contno2,riskcode3,2019-11-11",12.0);
//		m2.put("contno3,riskcode1,2019-12-11",11.2);
//		m2.put("contno3,riskcode2,2019-11-11",13.5);
//
//		boolean flag = true;
//		for ( String key : m1.keySet()){
//			flag = m2.containsKey(key);
//			if(flag){
//				if(m1.get(key).equals( m2.get(key))
//						){
//					flag = true;
//				}else {
//					flag = false;
//				}
//			}
//			if (!flag){
//				System.out.println(key+":"+m1.get(key));
//			}
//		}



//		String str = "102,23";
//		System.out.println(str.endsWith("3"));
//
//		String info = "S,02";
//		String flag = info.substring(0,1);
//		System.out.println(flag);


//		Set<FiDataTransDetail> fiDataCheckDetails1 = new HashSet<FiDataTransDetail>();
//		Set<FiDataTransDetail> fiDataCheckDetails2 = new HashSet<FiDataTransDetail>();
//
//		FiDataTransDetail a1 = new FiDataTransDetail();
//		FiDataTransDetail a2 = new FiDataTransDetail();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
//		try {
//			Date d1 = sdf.parse("2019-10-23");
//			Date d2 = sdf.parse("2019-10-23");
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//		a1.setAccountCode("6032");
//		a1.setContNo("1001");
//		a1.setSumMoney(100);
//
//		a2.setAccountCode("6032");
//		a2.setContNo("1001");
//		a2.setSumMoney(100);
//		fiDataCheckDetails1.add(a1);
//		fiDataCheckDetails2.add(a2);

//		Set<FiDataTransDetail> check = new HashSet<FiDataTransDetail>();
//		check.addAll(fiDataCheckDetails1);
//		check.removeAll(fiDataCheckDetails2);


//		System.out.println("a1.getContNo=a2.getContNo:" + (a1.getContNo() == a2.getContNo()));
//		System.out.println("a1.getContNo.equals(a2.getContNo):" + a1.getContNo().equals(a2.getContNo()));

//		for (int n = 0; n<fiDataCheckDetails1.size();n++){
//			System.out.println(fiDataCheckDetails1.contains(fiDataCheckDetails2));
//		}

//        String str = "String";
//		System.out.println(str.substring(2));

	}

}
